<template>
  <div class="page-dialog">
    <el-dialog
      v-model="dialogVisible"
      title="新增用户"
      width="30%"
      destroy-on-close
      center
    >
      <BBForm v-bind="dialogConfig" v-model="formData"></BBForm>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirmClick">确认</el-button>
        </span>
      </template>
      <!-- 插槽，树形控件 -->
      <slot></slot>
    </el-dialog>
  </div>
</template>

<script>
import { defineComponent, ref, watch } from "vue";
import BBForm from "@/base-ui/form/index";
import { useStore } from '@/store'
export default defineComponent({
  name: "PageDialog",
  components: {
    BBForm,
  },
  props: {
    dialogConfig: {
      type: Object,
      require: true,
    },
    defaultInfo: {
      type: Object,
      default: () => ({}),
    },
    pageName: {
      type: String,
      require: true,
    },
    //角色的权限控制的数据
    otherInfo :{
      type:Object,
      default:()=>({})
    }
  },
  setup(props) {
    const formData = ref({});
    const dialogVisible = ref(false);
    const store = useStore()
    //对 defaultInfo 进行一个观察，有值的话给 formData 进行赋值
    watch(
      () => props.defaultInfo,
      (newValue) => {
        for (const item of props.dialogConfig.formItems) {
          // 根据 item.filed 进行赋值
          formData.value[`${item.filed}`] = newValue[`${item.filed}`];
        }
      }
    );

    // 点击创建用户，点击按钮
    const handleConfirmClick = () => {
      //确定之后关闭弹出层
      dialogVisible.value = false;
      //发起网络请求
      // let menuList
      // if(props.otherInfo){
      //   menuList = JSON.stringify(props.otherInfo)
      // }else{
      //   menuList = ({})
      // }

      // const menuList = JSON.stringify(props.otherInfo)
        
        
        // console.log(menuList);
      if (Object.keys(props.defaultInfo).length) {
        //编辑
        store.dispatch('system/editPageListAction',{
          pageName:props.pageName,
          id:props.defaultInfo.id,
          editData:{...formData.value,...props.otherInfo}
        })
      
      } else {
        // 新建
       store.dispatch('system/createPageListAction',{
        pageName:props.pageName,
        newData:{...formData.value, ...props.otherInfo}
       })
       
        
      }
    };

    return {
      dialogVisible,
      formData,
      BBForm,
      handleConfirmClick,
    };
  },
});
</script>

<style>
</style>